package vn.aptech.scrumgroup.model;

import vn.aptech.scrumgroup.util.DrawUtil;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import demo.axitdn.physic.Unit;

public class SprintBurndownModel extends AbstractBaseModel{
	private int width;
	private int height;
	private Point startPoint;
	private Path path;

	float boundLeft;
	float boundRight;
	float boundTop;
	float boundBottom;

	public SprintBurndownModel(Point startPoint, int width, int height, float charWidth, int backColor, int foreColor){
		super(backColor, foreColor);
		this.width = width;
		this.height = height;
		this.startPoint = startPoint;		
	}
	@Override
	public void onDraw(Canvas c) {
		// TODO Auto-generated method stub		
		//draw sprint burndown
		path = new Path();
		path.moveTo(startPoint.x, startPoint.y);
		path.lineTo(startPoint.x, startPoint.y - height);
		path.moveTo(startPoint.x, startPoint.y);
		path.lineTo(startPoint.x + width, startPoint.y);
		boundLeft = startPoint.x;
		
		path.moveTo(startPoint.x, startPoint.y - height);
		path.lineTo(startPoint.x - Unit.i * 5, startPoint.y - height + Unit.j * 5);
		path.moveTo(startPoint.x, startPoint.y - height);
		path.lineTo(startPoint.x + Unit.i * 5, startPoint.y - height + Unit.j * 5);
		boundTop = startPoint.y - height;
		
		path.moveTo(startPoint.x + width, startPoint.y);
		path.lineTo(startPoint.x + width - Unit.i * 5, startPoint.y - Unit.j * 5);
		path.moveTo(startPoint.x + width, startPoint.y);
		path.lineTo(startPoint.x + width - Unit.i * 5, startPoint.y + Unit.j * 5);
		boundRight = startPoint.x + width;
		boundBottom = startPoint.y;
		
		float partWidth = width / 6;
		float partHeight = height / 16;
		
		float xp = startPoint.x;
		float yp = startPoint.y - height + partHeight;
		
		float min = Unit.i;
		float max = partWidth;
		for(int i = 0; i < 5; i ++){
			path.moveTo(xp, yp);
			yp = yp + partHeight;
			xp = xp + (min + (float)(0.5f * ((max - min) + 1)));
			path.lineTo(xp, yp);
		}
		
		for(int i = 0; i < 8; i ++){
			path.moveTo(xp, yp);
			yp = yp + partHeight;
			xp = xp + (min + (float)(0.2f * ((max - min) + 1)));
			path.lineTo(xp, yp);
		}
		
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		//paint.setColor(Color.parseColor("#06276F"));
		paint.setColor(backColor);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(Unit.i);
		c.drawLine(startPoint.x, startPoint.y - height + partHeight, startPoint.x + width - partWidth, startPoint.y, paint);
		
		paint.setColor(Color.parseColor("#06276F"));
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(Unit.i);
		c.drawPath(path, paint);
		
		//paint.setColor(Color.parseColor("#FF5600"));
		paint.setColor(foreColor);
		paint.setTextAlign(Align.CENTER);
		drawText(c, "Burndown", startPoint.x + width / 2, startPoint.y + width / 10, paint, 0.0f);
		
	}

	private void drawText(Canvas canvas, String text, float x, float y, Paint paint, float charWidth)
	{
		paint.setStyle(Style.FILL_AND_STROKE);
		if(charWidth > 0){
			paint.setStrokeWidth(charWidth);
		}
//		paint.setTextSize(width * 0.1f);
		paint.setTextSize(DrawUtil.calculateTextSize(text, boundRight-boundLeft - 33*Unit.i, paint, 3));
		paint.setAntiAlias(true);
		Rect r = new Rect();
		paint.getTextBounds(text, 0, text.length(), r);
		canvas.drawText(text, x, y + r.height()/2, paint);
	}
	
	@Override
	public void onUpdate(double t) {
		// TODO Auto-generated method stub
		
	}

	public boolean isPointOf(float x, float y) {
		// TODO Auto-generated method stub
		if(x <= boundRight && x >= boundLeft)
			if(y <= boundBottom && y >= boundTop)
				return true;
		return false;
	}
	@Override
	public void changeColor(int color) {
		// TODO Auto-generated method stub
		
	}
}